/*
 * @Description: 用户
 * @Author: 0001100986/LiuShuai
 * @Date: 2021-06-07 14:15:35
 * @LastEditTime: 2021-06-07 19:30:19
 * @LastEditors: 0001100986/LiuShuai
 */

const state = {
  user: {
    // 语言
    language: 'cn',
    // 是否登录
    isLogin: false,
    // 用户信息
    info: {},
    // 选项卡
    worktab: {
      // 当前
      current: {},
      // 打开的
      opened: []
    },
    // 个性化设置及其它设置
    setting: {}
  }
};

const getters = {
  user: (state) => state.user
};

const mutations = {
  // 初始化state
  initState () {
    let sys = JSON.parse(localStorage.getItem('sys'));
    if (sys && sys.user.info.ID) {
      state.user.info = sys.user.info;
      state.user.isLogin = sys.user.isLogin;
    }
  },
  // 用户数据持久化存储
  storeStorage (state, rootState) {
    state.user.worktab = rootState.worktab.worktab;
    state.user.setting = rootState.setting.setting;
    saveStoreStorage(state);
  },
  // 设置登录状态
  setLoginStatus (state, isLogin) {
    state.user.isLogin = isLogin;

    let sys = JSON.parse(localStorage.getItem('sys'));
    if (sys) {
      sys.user.isLogin = isLogin;
      saveStoreStorage(sys);
    }
  },
  // 设置语言
  setLanguage (state, lang) {
    state.user.language = lang;

    let sys = JSON.parse(localStorage.getItem('sys'));
    if (sys) {
      sys.user.language = lang;
      saveStoreStorage(sys);
    }
  },
  // 设置用户信息
  setUserInfo (state, e) {
    state.user.info = e.user;
    state.user.info.token = e.token;
    let sys = JSON.parse(localStorage.getItem('sys'));

    if (sys) {
      sys.user.info = e.user;
      sys.user.info.token = e.token;
      saveStoreStorage(sys);
    } else {
      sys = {
        user: state.user
      };
      saveStoreStorage(sys);
    }
  }
};

const actions = {
  initState ({ commit }) {
    commit('initState');
  },
  storeStorage ({ commit, rootState }) {
    commit('storeStorage', rootState);
  },
  setLoginStatus ({ commit }, e) {
    commit('setLoginStatus', e);
  },
  setLanguage ({ commit }, e) {
    commit('setLanguage', e);
  },
  setUserInfo ({ commit }, e) {
    commit('setUserInfo', e);
  }
};

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
};

function saveStoreStorage (sys) {
  localStorage.setItem('sys', JSON.stringify(sys));
}